﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class UpdatePetitionReceive : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_Petitions pet = new B_FIM_Petitions();
        IBLL_FIM_PetitionDtls petd = new B_FIM_PetitionDtls();
        IBLL_FIM_Receives r = new B_FIM_Receives();
        IBLL_FIM_Payments pay = new B_FIM_Payments();
        IBLL_FIM_MAS_BankAccounts acc = new B_FIM_MAS_BankAccounts();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void LoadData()
        {
            M_FIM_Petitions info = pet.GetInfo(Session["PetitionSeq"].ToString());
            lblPetitionNo1.Text = info.petition_no;
            lblPetitionNo2.Text = info.petition_no;
            lblBgmSourceSeq.Text = info.bgm_source_seq.ToString();

            if (info.direct_pay == "0")
            {
                PanelReceive.Visible = true;
                PanelPayment.Visible = false;
                LoadDDLBankAccSeq(info.bgm_source_seq.ToString());

                M_FIM_Receives recieveInfo = r.GetInfoFromPetition(Session["PetitionSeq"].ToString());

                if (recieveInfo.receive_date == null)
                {
                    dpReceiveDate.SetValue(DateTime.Today);
                    lblReceiveSeq.Text = "";
                }
                else
                {
                    dpReceiveDate.SetValue(recieveInfo.receive_date);
                    ddlBankAccSeq.SelectedValue = recieveInfo.bank_acc_seq.ToString();
                    lblReceiveSeq.Text = recieveInfo.receive_seq.ToString();
                }

                lblBgmYear1.Text = info.bgm_year.ToString();
                lblBgmSourceName1.Text = info.bgm_source_name;
            }
            else
            {
                PanelReceive.Visible = false;
                PanelPayment.Visible = true;

                IList<M_FIM_Payments> PayInfo = pay.GetListFromPetition(Session["PetitionSeq"].ToString());

                if (PayInfo.Count == 0)
                {
                    dpRealPayDate.SetValue(DateTime.Today);
                }
                else
                {
                    dpRealPayDate.SetValue(PayInfo[0].real_pay_date);
                }

                lblBgmYear2.Text = info.bgm_year.ToString();
                lblBgmSourceName2.Text = info.bgm_source_name;
                grdPetitionDtls.DataSource = petd.GetFull(Session["PetitionSeq"].ToString());
                grdPetitionDtls.DataBind();
            }

            lblAmount.Text = info.sum_amount.Value.ToString("#,##0.00");
            lblTax.Text = info.sum_amount_tax.Value.ToString("#,##0.00");
            lblRedemption.Text = info.sum_amount_redemption.Value.ToString("#,##0.00");
            lblPenalty.Text = info.sum_amount_penalty.Value.ToString("#,##0.00");
            lblAmountReal.Text = info.sum_amount_real.Value.ToString("#,##0.00");
        }

        protected void LoadDDLBankAccSeq(string bgm_source_seq)
        {
            ddlBankAccSeq.DataSource = acc.GetList(null, bgm_source_seq, null, null);
            ddlBankAccSeq.DataBind();

            main.InsertEmptyDDLSelect(ddlBankAccSeq);
            ddlBankAccSeq.SelectedIndex = 0;
        }

        protected void cmdOK_Click(object sender, EventArgs e)
        {
            if (PanelReceive.Visible)
            {
                if (!CheckData1())
                {
                    return;
                }

                M_FIM_Receives receiveInfo = new M_FIM_Receives();

                if (lblReceiveSeq.Text.Length == 0)
                {
                    receiveInfo.bgm_source_seq = main.ConvertNullInt(lblBgmSourceSeq.Text);
                    receiveInfo.bgm_year = main.ConvertNullInt(lblBgmYear1.Text);
                    receiveInfo.receive_date = dpReceiveDate.GetDateValue();
                    receiveInfo.receive_mode = "3";
                    receiveInfo.method_seq = 3;
                    receiveInfo.bank_acc_seq = main.ConvertNullInt(ddlBankAccSeq.SelectedValue);
                    receiveInfo.emp_seq = main.ConvertNullInt(Session["EmpSeq"]);
                    receiveInfo.amount = main.ConvertNullDouble(lblAmountReal.Text);
                    //receiveInfo.currency_seq = main.GetSystemControls().default_currency_seq;
                    //receiveInfo.exchange_rate = 1;
                    //receiveInfo.bath_amount = main.ConvertNullDouble(lblAmountReal.Text);
                    receiveInfo.petition_seq = Convert.ToInt32(Session["PetitionSeq"]);
                    receiveInfo.record_status = "A";
                    receiveInfo.created_by = main.ConvertNullInt(Session["EmpSeq"]);
                    receiveInfo.created_when = DateTime.Now;
                    r.Insert(receiveInfo);
                }
                else
                {
                    receiveInfo.receive_seq = Convert.ToInt32(lblReceiveSeq.Text);
                    receiveInfo.bgm_source_seq = main.ConvertNullInt(lblBgmSourceSeq.Text);
                    receiveInfo.bgm_year = main.ConvertNullInt(lblBgmYear1.Text);
                    receiveInfo.receive_date = dpReceiveDate.GetDateValue();
                    receiveInfo.receive_mode = "3";
                    receiveInfo.method_seq = 3;
                    receiveInfo.bank_acc_seq = main.ConvertNullInt(ddlBankAccSeq.SelectedValue);
                    receiveInfo.emp_seq = main.ConvertNullInt(Session["EmpSeq"]);
                    receiveInfo.amount = main.ConvertNullDouble(lblAmountReal.Text);
                    receiveInfo.currency_seq = main.GetSystemControls().default_currency_seq;
                    receiveInfo.exchange_rate = 1;
                    receiveInfo.bath_amount = main.ConvertNullDouble(lblAmountReal.Text);
                    receiveInfo.petition_seq = Convert.ToInt32(Session["PetitionSeq"]);
                    receiveInfo.record_status = "A";
                    receiveInfo.updated_by = main.ConvertNullInt(Session["EmpSeq"]);
                    receiveInfo.updated_when = DateTime.Now;
                    r.Update(receiveInfo);
                }
            }
            else
            {
                if (!CheckData2())
                {
                    return;
                }

                foreach (GridDataItem item in grdPetitionDtls.Items)
                {
                    M_FIM_Payments payInfo = new M_FIM_Payments();
                    string strPaySeq = main.WebString(item["pay_seq"].Text);
                    string strMoneyReqSeq = main.WebString(item["money_req_seq"].Text);
                    string strMoneyReqName = main.WebString(item["money_req_name"].Text);
                    string strEventSeq = main.WebString(item["event_seq"].Text);
                    string strPetitionDtlsSeq = main.WebString(item["petition_dtls_seq"].Text);
                    string strContactSeq = main.WebString(item["contact_seq"].Text);
                    string strAmount = main.WebString(item["amount_real"].Text);
                    string strTax = main.WebString(item["amount_tax"].Text);

                    if (strPaySeq.Length == 0)
                    {
                        payInfo.bgm_source_seq = Convert.ToInt32(lblBgmSourceSeq.Text);
                        payInfo.bgm_year = Convert.ToInt32(lblBgmYear2.Text);
                        payInfo.work_area_seq = Convert.ToInt32("1");
                        payInfo.event_seq = main.ConvertNullInt(strEventSeq);
                        payInfo.pay_date = dpRealPayDate.GetDateValue();
                        payInfo.real_pay_date = dpRealPayDate.GetDateValue();
                        payInfo.pay_name = "จ่ายตรง " + strMoneyReqName;
                        payInfo.method_seq = 3;
                        payInfo.money_req_seq = main.ConvertNullInt(strMoneyReqSeq);
                        payInfo.petition_dtls_seq = main.ConvertNullInt(strPetitionDtlsSeq);
                        payInfo.contact_seq = main.ConvertNullInt(strContactSeq);
                        payInfo.amount = main.ConvertNullDouble(strAmount);
                        payInfo.tax = main.ConvertNullDouble(strTax);

                        payInfo.record_status = "A";
                        payInfo.created_by = main.ConvertNullInt(Session["EmpSeq"]);
                        payInfo.created_when = DateTime.Now;
                        pay.Insert(payInfo);
                    }
                    else
                    {
                        payInfo.pay_seq = Convert.ToInt32(strPaySeq);
                        payInfo.bgm_source_seq = Convert.ToInt32(lblBgmSourceSeq.Text);
                        payInfo.bgm_year = Convert.ToInt32(lblBgmYear2.Text);
                        payInfo.work_area_seq = Convert.ToInt32("1");
                        payInfo.event_seq = main.ConvertNullInt(strEventSeq);
                        payInfo.pay_date = dpRealPayDate.GetDateValue();
                        payInfo.real_pay_date = dpRealPayDate.GetDateValue();
                        payInfo.pay_name = "จ่ายตรง " + strMoneyReqName;
                        payInfo.method_seq = 3;
                        payInfo.money_req_seq = main.ConvertNullInt(strMoneyReqSeq);
                        payInfo.petition_dtls_seq = main.ConvertNullInt(strPetitionDtlsSeq);
                        payInfo.contact_seq = main.ConvertNullInt(strContactSeq);
                        payInfo.amount = main.ConvertNullDouble(strAmount);
                        payInfo.tax = main.ConvertNullDouble(strTax);

                        payInfo.record_status = "A";
                        payInfo.updated_by = main.ConvertNullInt(Session["EmpSeq"]);
                        payInfo.updated_when = DateTime.Now;
                        pay.Update(payInfo);
                    }
                }
            }

            main.MessageAlert(Page, "บันทึกเรียบร้อย");
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "onclick"
                , "returnToParent()", true);
        }

        protected bool CheckData1()
        {
            bool bOK = true;
            string strReceiveDate = dpReceiveDate.GetStringValue();
            string strBankAccSeq = ddlBankAccSeq.SelectedValue;

            if (strReceiveDate.Length == 0)
            {
                bOK = false;
                main.MessageAlert(Page, "กรุณากรอกข้อมูล [วันที่วันที่ได้รับเงิน]");
            }
            else if (strBankAccSeq.Length == 0)
            {
                bOK = false;
                main.MessageAlert(Page, "กรุณากรอกข้อมูล [โอนเข้าบัญชีเลขที่]");
            }

            return bOK;
        }

        protected bool CheckData2()
        {
            bool bOK = true;
            string strRealPayDate = dpRealPayDate.GetStringValue();

            if (strRealPayDate.Length == 0)
            {
                bOK = false;
                main.MessageAlert(Page, "กรุณากรอกข้อมูล [วันที่จ่ายตรง]");
            }

            return bOK;
        }
    }
}